package model;

import java.util.ArrayList;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import utilities.HibernateUtil;
import entity.Web;

public class WebDAO {

    /**
     * Load Web in Shop.
     * @return List Web
     * @throws Exception
     */
    @SuppressWarnings({"unchecked", "finally"})
    public  ArrayList<Web> getList() throws Exception {
        ArrayList<Web> list = new ArrayList<Web>();
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            list = (ArrayList<Web>) session.createCriteria(Web.class).list();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {

        }
        session.close();
        return list;
    }

    /**
     * Add New Web
     * @param Web
     * @throws Exception
     */
    public static void add(Web web) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.save(web);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Update Web
     * @param Web
     * @throws Exception
     */
    public static void update(Web web) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.update(web);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Delete Web
     * @param Web
     * @throws Exception
     */
    public static void delete(Web web) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();
            session.delete(web);
            transaction.commit();
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
        }
    }

    /**
     * Find Web by ID
     * @param id
     * @return Web
     * @throws Exception
     */
    @SuppressWarnings("finally")
    public static Web find(int id) throws Exception {
        SessionFactory sessionFactory = null;
        Session session = null;
        Web Web = new Web();
        try {
            sessionFactory = HibernateUtil.getSessionFactory();
            session = sessionFactory.openSession();
            Web = (Web) session.get(Web.class, id);
        } catch (Exception e) {
            throw new Exception(e);
        } finally {
            session.close();
            return Web;
        }
    }

}
